package logic

import (
	"context"
	"errors"
	"shop_srv/model/redisinit"

	"shop_srv/internal/svc"
	"shop_srv/shop"

	"github.com/zeromicro/go-zero/core/logx"
)

type CartTotalLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCartTotalLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CartTotalLogic {
	return &CartTotalLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 计算总数量和价格
func (l *CartTotalLogic) CartTotal(in *shop.CartTotalRequest) (*shop.CartTotalResponse, error) {
	// todo: add your logic here and delete this line

	total, f, err := redisinit.CartTotal(in.UserId)
	if err != nil {
		logx.Error("计算总数量和总价格失败")
		return nil, errors.New("计算总数量和总价格失败")
	}
	return &shop.CartTotalResponse{
		Count: total,
		Sum:   float32(f),
	}, nil
}
